package com.coship.vasms.adstorm.common;

import javax.sql.DataSource;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * 
 * 
 * File Name : DataSourceFactoryBean.java
 * 
 * @Description : 扩展的spring数据源bean 从应用服务器的config目录下读取数据库连接信息 这样方便修改数据库的配置信息
 * @author 904120
 */
public class DataSourceFactoryBean implements FactoryBean, InitializingBean {
	/** * logger. */
	private static final Logger logger = Logger
			.getLogger(DataSourceFactoryBean.class);

	/** 缓存数据源对象 */
	private DataSource dataSource;

	/** jdbc驱动类 */
	private String driverClassName;

	/** 数据库的url */
	private String url;

	/** 用户名 */
	private String username;

	/** 密码 */
	private String password;
	/**最大连接数量**/
	private int maxActive;
	/**最大空闲连接**/
	private int maxIdle;
	/** 初始化连接 **/
	private int initialSize;

	/**
	 * 初始化配置文件,目前只支持Properties类型文件
	 * 
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		// 读配置文件
		Configuration configuration = new PropertiesConfiguration(CommonUtils
				.getConfigPath()
				+ CommonUtils.getSysFileSeparator() + "jdbc.properties");

		if (configuration == null) {
			logger.error("数据源配置文件jdbc.properties不存在!");
			throw new IllegalArgumentException("数据源配置文件jdbc.properties不存在!");
		}

		driverClassName = configuration.getString("jdbc.driverClassName");
		url = configuration.getString("jdbc.url");
		username = configuration.getString("jdbc.username");
		password = configuration.getString("jdbc.password");
		maxActive = configuration.getInt("dbcp.maxActive");
		maxIdle = configuration.getInt("dbcp.maxIdle");
		initialSize = configuration.getInt("dbcp.initialSize");

		if (StringUtils.isEmpty(this.driverClassName)) {
			logger.error("数据源配置项driverClassName不存在!");
			throw new IllegalArgumentException("数据源配置项driverClassName不存在!");
		}

		if (StringUtils.isEmpty(this.url)) {
			logger.error("数据源配置项url不存在!");
			throw new IllegalArgumentException("数据源配置项url不存在!");

		}

		if (StringUtils.isEmpty(this.username)) {
			logger.error("数据源配置项username不存在!");
			throw new IllegalArgumentException("数据源配置项username不存在!");

		}

		if (StringUtils.isEmpty(this.password)) {
			logger.error("数据源配置项password不存在!");
			throw new IllegalArgumentException("数据源配置项password不存在!");

		}

		// 在初始化时就创建数据源
		createDataSource();
		System.out.println("创建数据源成功");
	}

	/**
	 * 重载spring方法.
	 * 
	 * @see org.springframework.beans.factory.FactoryBean#getObject()
	 */
	public Object getObject() {
		DataSource ds = this.createDataSource();
		return ds;
	}

	/**
	 * 创建数据源对象．
	 */
	protected DataSource createDataSource() {
		DataSource ds = this.dataSource;
		if (ds == null) {
			try {
				// 使用apache的dbcp数据源提高测试性能
				BasicDataSource basicDataSource = new BasicDataSource();
				basicDataSource.setDriverClassName(driverClassName);
				basicDataSource.setUrl(url);
				basicDataSource.setUsername(username);
				basicDataSource.setPassword(password);
				basicDataSource.setMaxActive(maxActive);
				basicDataSource.setMaxIdle(maxIdle);
				basicDataSource.setInitialSize(initialSize);

				ds = basicDataSource;

			} catch (Throwable err) {
				throw new RuntimeException(err);
			}
		}
		this.dataSource = ds;
		return ds;
	}

	/**
	 * 重载spring方法.
	 * 
	 * @see org.springframework.beans.factory.FactoryBean#getObjectType()
	 */
	public Class getObjectType() {
		return javax.sql.DataSource.class;

	}

	/**
	 * 重载spring方法.
	 * 
	 * @see org.springframework.beans.factory.FactoryBean#isSingleton()
	 */
	public boolean isSingleton() {
		return true;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getMaxActive()
	{
		return maxActive;
	}

	public void setMaxActive(int maxActive)
	{
		this.maxActive = maxActive;
	}

	public int getMaxIdle()
	{
		return maxIdle;
	}

	public void setMaxIdle(int maxIdle)
	{
		this.maxIdle = maxIdle;
	}

	public int getInitialSize()
	{
		return initialSize;
	}

	public void setInitialSize(int initialSize)
	{
		this.initialSize = initialSize;
	}
}
